home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Amiga / _open.c < prev    next >
C/C++ Source or Header  |  1998-01-21  |  3KB  |  156 lines

  1. RCS_ID_C="$Id: _open.c,v 4.1 1994/09/29 23:09:02 jraja Exp $";
  2. /*
  3.  *      _open.c - Unix compatible open() (SAS/C)
  4.  *
  5.  *      Copyright © 1994 AmiTCP/IP Group, 
  6.  *                       Network Solutions Development Inc.
  7.  *                       All rights reserved.
  8.  */
  9.  
  10. #include <ios1.h>
  11. #include <fcntl.h>
  12. #include <stdlib.h>
  13. #include <dos.h>
  14. #include <string.h>
  15. #include <errno.h>
  16. #include <dos/dos.h>
  17. #include <proto/dos.h>
  18. #include <proto/usergroup.h>
  19. #include <stdarg.h>
  20. #include <unistd.h>
  21.  
  22. #include <bsdsocket.h>
  23. #include "Python.h"
  24.  
  25. #include "netlib.h"
  26.  
  27. extern int (*__closefunc)(int);
  28.  
  29. __stdargs int
  30. __open(const char *name, int mode, ...)
  31. {
  32.   struct UFB *ufb;
  33.   int         fd;
  34.   int         flags;
  35.   char        newfile = TRUE;
  36.   BPTR        file;
  37.  
  38.   /*
  39.    * Set up __closefunc (which is used at cleanup)
  40.    */
  41.   __closefunc = __close;
  42.  
  43.   /*
  44.    * Check for the break signals
  45.    */
  46.   __chkabort();
  47.  
  48.   /*
  49.    * find first free ufb
  50.    */
  51.   ufb = __allocufb(&fd);
  52.   if (ufb == NULL)
  53.     return -1; /* errno is set by the __allocufb() */
  54.  
  55.   /*
  56.    * malloc space for the name & copy it
  57.    */
  58.   if ((ufb->ufbfn = malloc(strlen(name)+1)) == NULL) {
  59.     SET_OSERR(ERROR_NO_FREE_STORE);
  60.     errno = ENOMEM;
  61.     return -1;
  62.   }
  63.   strcpy(ufb->ufbfn, name);
  64.   /*
  65.    * Translate mode to ufb flags
  66.    */
  67.   switch (mode & (O_WRONLY | O_RDWR)) {
  68.   case O_RDONLY:
  69.     if (mode & (O_APPEND | O_CREAT | O_TRUNC | O_EXCL)) {
  70.       errno = EINVAL;
  71.       return -1;
  72.     }
  73.     flags = UFB_RA;
  74.     break;
  75.   case O_WRONLY:
  76.     flags = UFB_WA;
  77.     break;
  78.   case O_RDWR:
  79.     flags = UFB_RA | UFB_WA;
  80.     break;
  81.   default:
  82.     errno = EINVAL;
  83.     return -1;
  84.   }
  85.   if (mode & O_APPEND)
  86.     flags |= UFB_APP;
  87.   if (mode & O_XLATE)
  88.     flags |= UFB_XLAT;
  89.   if (mode & O_TEMP)
  90.     flags |= UFB_TEMP;
  91.   if (mode & O_CREAT) {
  92.     BPTR lock;
  93.     if (lock = Lock((char *)name, SHARED_LOCK)) {
  94.       if (mode & O_EXCL) {
  95.     UnLock(lock);
  96.     errno = EEXIST;
  97.     free(ufb->ufbfn);
  98.     return -1;
  99.       }
  100.  
  101.       if (mode & O_TRUNC)
  102.     newfile = FALSE;
  103.       else
  104.     mode &= ~O_CREAT;
  105.  
  106.       UnLock(lock);
  107.     }
  108.   }
  109.   if (mode & O_CREAT) {
  110.     if ((file = Open((char *)name, MODE_NEWFILE)) == NULL)
  111.       goto osfail;
  112.  
  113.     if (newfile) {
  114.       va_list va;
  115.       int cmode;
  116.  
  117.       va_start(va, mode);
  118.  
  119.     /* needs usergroup.library -- I.J. */
  120.     if(!checkusergrouplib())
  121.     {
  122.         PyErr_Clear();
  123.         cmode=va_arg(va,int);        /* don't use getumask() */
  124.     }
  125.     else
  126.         cmode = va_arg(va, int) & ~getumask();
  127.       
  128.       chmod((char *)name, cmode); /* hope this doesn't fail :-) */
  129.     }
  130.   }
  131.   else {
  132.     if ((file = Open((char *)name,
  133.              (flags & UFB_WA && mode & O_LOCK) ? 
  134.              MODE_READWRITE : MODE_OLDFILE)) == NULL)
  135.       goto osfail;
  136.   }
  137.  
  138.   /*
  139.    * All done! Setting the ufb->ufbflg field to non-zero value marks
  140.    * this ufb used. 
  141.    */
  142.   ufb->ufbflg = flags;
  143.   ufb->ufbfh = (long)file;
  144.  
  145.   return fd;
  146.  
  147. osfail:
  148.   {
  149.     int code = IoErr();
  150.     if (ufb->ufbfn)
  151.       free(ufb->ufbfn);
  152.     set_errno(code);
  153.   }
  154.   return -1;
  155. }
  156.